import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.parser.SqlParseException;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.Planner;
import org.apache.calcite.tools.ValidationException;

public class SQLParseTest {
    public static void main(String[] args) throws SqlParseException, ValidationException {
        // 初始化配置

        String sql = "select * from emp";
        SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();

        //configBuilder.setUnquotedCasing(Casing.UNCHANGED);

//Sql解析：解析Sql语句，通过JavaCC解析成AST语法树，表现为SqlNode

        SqlParser sqlParser = SqlParser.create(sql, configBuilder.build());

        SqlNode sqlNode = sqlParser.parseQuery();

//Sql校验：结合元数据信息验证Sql是否符合规范
        SqlParser.Config insensitiveParser = SqlParser.configBuilder()
                .setCaseSensitive(false)
                .build();

        SchemaPlus rootSchema = Frameworks.createRootSchema(true);

        FrameworkConfig config = Frameworks.newConfigBuilder()
                .parserConfig(insensitiveParser)
                .defaultSchema(rootSchema)
                .build();

        Planner planner = Frameworks.getPlanner(config);

        SqlNode node = planner.validate(sqlNode);

    }
}
